<!DOCTYPE html>
<html lang="zh" class="js csstransforms3d">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="Hugo 0.93.0" />
    <meta name="description" content="Documentation for Hugo Learn Theme">
<meta name="author" content="Mathieu Cornic">

    <link rel="icon" href="/images/favicon.png" type="image/png">

    <title>Node博客系统开发记录 :: welcome to my blog</title>

    
    <link href="/css/nucleus.css?1648731304" rel="stylesheet">
    <link href="/css/fontawesome-all.min.css?1648731304" rel="stylesheet">
    <link href="/css/hybrid.css?1648731304" rel="stylesheet">
    <link href="/css/featherlight.min.css?1648731304" rel="stylesheet">
    <link href="/css/perfect-scrollbar.min.css?1648731304" rel="stylesheet">
    <link href="/css/auto-complete.css?1648731304" rel="stylesheet">
    <link href="/css/atom-one-dark-reasonable.css?1648731304" rel="stylesheet">
    <link href="/css/theme.css?1648731304" rel="stylesheet">
    <link href="/css/tabs.css?1648731304" rel="stylesheet">
    <link href="/css/hugo-theme.css?1648731304" rel="stylesheet">
    
    <link href="/css/theme-green.css?1648731304" rel="stylesheet">
    
    

    <script src="/js/jquery-3.3.1.min.js?1648731304"></script>

    <style>
      :root #header + #content > #left > #rlblock_left{
          display:none !important;
      }
      
    </style>
    
  </head>
  <body class="" data-url="/node.js/node%E5%8D%9A%E5%AE%A2%E7%B3%BB%E7%BB%9F/">
    <nav id="sidebar" class="showVisitedLinks">



  <div id="header-wrapper">
    <div id="header">
      <a id="logo" href='/'>

<svg id="grav-logo" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
	width="100%" height="100%"  viewBox="0 0 285.1 69.9" xml:space="preserve">
<g>
	<path d="M35,0c9.6,0,17.9,3.4,24.7,10.3c6.8,6.8,10.3,15,10.3,24.7c0,9.7-3.4,17.9-10.3,24.7c-6.8,6.8-15,10.3-24.7,10.3
		c-9.7,0-17.9-3.4-24.7-10.3C3.4,52.8,0,44.6,0,34.9c0-9.6,3.4-17.9,10.3-24.7C17.1,3.4,25.3,0,35,0z M22.3,63.7
		c0.3-7.4,0.3-14.7,0-22.1c-2.1-0.4-4-0.4-5.9,0c-0.2,0.8-0.2,2.5-0.1,4.9c0.1,2.3,0,3.9-0.3,4.8c-1.7-0.6-2.7-2.1-3.1-4.6
		c-0.1-0.7-0.1-3.3-0.1-8c0-14.5-0.1-23-0.3-25.6c-3.6,4.1-5.6,7-5.9,8.5v10.9c0,4.5,0.1,8.1,0.2,10.9c0,0.4,0,1.3-0.1,2.8
		c-0.1,1.2,0,2.1,0.1,2.6c0.1,0.6,1.1,2.1,2.8,4.7C13.2,58.7,17.5,62.2,22.3,63.7z M44.6,65.4c0.3-5,0.2-12.9-0.1-23.6
		c-2.1-0.3-4.1-0.3-5.9,0c-0.2,2-0.2,5.1-0.1,9.5c0.1,4.3,0,7.5-0.1,9.5c-2.9,0.4-5,0.3-6.2-0.1c-0.2-5.6-0.2-15.2-0.1-28.7
		c0.1-12.4,0-21.8-0.3-28.3c-2.4,0.1-4.3,0.6-5.7,1.3c-0.1,7.7-0.2,17.7-0.1,30.1c0,16.5,0,26.6,0,30.3c2.3,1.1,5.4,1.7,9.4,1.7
		C38.9,66.9,42.1,66.4,44.6,65.4z M48.1,64.1c9.7-4.6,15.7-12,18-22.2c-1.8-0.2-3.8-0.3-6.1-0.3c-1.6,3.9-2.6,6.3-3.2,7.2
		c-1.1,1.7-2,2.5-2.7,2.5C54,46,54,39.1,54,30.5c0-11.5,0-18.4,0-20.9c-1.4-1.4-3.3-2.5-5.7-3.4C48.1,6.3,48,6.4,48,6.7
		c0,3.5,0,13.1,0,28.8C47.9,47.2,48,56.8,48.1,64.1z"/>
</g>
<g>
	<path d="M116.6,51.3h-29c-0.5,0-0.9-0.1-1.3-0.2c-0.4-0.2-0.7-0.4-1-0.7c-0.3-0.3-0.5-0.6-0.7-1c-0.2-0.4-0.2-0.8-0.2-1.3V16.3h6.3
		V45h25.8V51.3z"/>
	<path d="M154.8,51.3h-22.9c-0.9,0-1.8-0.2-2.9-0.5c-1-0.3-2-0.8-2.9-1.5c-0.9-0.7-1.6-1.6-2.2-2.8c-0.6-1.1-0.9-2.5-0.9-4.2V19.5
		c0-0.4,0.1-0.9,0.2-1.2c0.2-0.4,0.4-0.7,0.7-1c0.3-0.3,0.6-0.5,1-0.7c0.4-0.2,0.8-0.2,1.3-0.2h28.6v6.3h-25.4v19.8
		c0,0.8,0.2,1.5,0.7,1.9s1.1,0.7,1.9,0.7h22.9V51.3z M151.9,37h-20v-6.4h20V37z"/>
	<path d="M197.3,51.3H191v-8.6h-22.3v8.6h-6.3V33.8c0-2.6,0.4-4.9,1.3-7.1s2.1-4,3.7-5.5c1.6-1.5,3.4-2.8,5.5-3.6
		c2.1-0.9,4.5-1.3,7-1.3h14.3c0.4,0,0.9,0.1,1.2,0.2c0.4,0.2,0.7,0.4,1,0.7s0.5,0.6,0.7,1c0.2,0.4,0.2,0.8,0.2,1.2V51.3z
		 M168.7,36.4H191V22.6h-11.2c-0.2,0-0.6,0-1.2,0.1c-0.6,0.1-1.4,0.2-2.2,0.4c-0.8,0.2-1.7,0.6-2.6,1c-0.9,0.5-1.8,1.1-2.5,2
		c-0.8,0.8-1.4,1.9-1.9,3.1c-0.5,1.2-0.7,2.8-0.7,4.5V36.4z"/>
	<path d="M241.7,28.1c0,1.4-0.2,2.7-0.5,3.9c-0.4,1.1-0.8,2.1-1.5,3c-0.6,0.9-1.3,1.6-2.1,2.2c-0.8,0.6-1.6,1.1-2.5,1.5
		c-0.9,0.4-1.8,0.7-2.6,0.9c-0.9,0.2-1.7,0.3-2.5,0.3l13.3,11.5h-9.8l-13.2-11.5h-4.6v-6.3H230c0.8-0.1,1.5-0.2,2.2-0.5
		c0.7-0.3,1.2-0.6,1.7-1.1c0.5-0.5,0.9-1,1.1-1.6c0.3-0.6,0.4-1.4,0.4-2.2v-4c0-0.4,0-0.6-0.1-0.8c-0.1-0.2-0.2-0.3-0.3-0.4
		c-0.1-0.1-0.3-0.1-0.4-0.2c-0.2,0-0.3,0-0.4,0h-20.9v28.7h-6.3V19.5c0-0.4,0.1-0.9,0.2-1.2c0.2-0.4,0.4-0.7,0.7-1
		c0.3-0.3,0.6-0.5,1-0.7c0.4-0.2,0.8-0.2,1.3-0.2H234c1.4,0,2.6,0.3,3.6,0.8s1.8,1.2,2.4,1.9c0.6,0.8,1,1.6,1.3,2.5
		c0.3,0.9,0.4,1.7,0.4,2.5V28.1z"/>
	<path d="M285.1,48.6c0,0.5-0.1,0.9-0.3,1.3c-0.2,0.4-0.4,0.7-0.7,1c-0.3,0.3-0.6,0.5-1,0.7c-0.4,0.2-0.8,0.2-1.2,0.2
		c-0.4,0-0.8-0.1-1.2-0.2c-0.4-0.1-0.8-0.4-1.1-0.7l-23.2-24.2v24.7h-6.3V19c0-0.7,0.2-1.2,0.5-1.8c0.4-0.5,0.8-0.9,1.4-1.2
		c0.6-0.2,1.2-0.3,1.9-0.2s1.2,0.4,1.6,0.9l23.2,24.2V16.3h6.3V48.6z"/>
</g>
</svg>

</a>

    </div>
    
        <div class="searchbox">
    <label for="search-by"><i class="fas fa-search"></i></label>
    <input data-search-input id="search-by" type="search" placeholder="">
    <span data-search-clear=""><i class="fas fa-times"></i></span>
</div>

<script type="text/javascript" src="/js/lunr.min.js?1648731304"></script>
<script type="text/javascript" src="/js/auto-complete.js?1648731304"></script>
<script type="text/javascript">
    
        var baseurl = "\/";
    
</script>
<script type="text/javascript" src="/js/search.js?1648731304"></script>

    
  </div>
  

    <div class="highlightable">
    <ul class="topics">

        
          
          




 
  
    
    <li data-nav-id="/javascript/" title="— JavaScript" class="dd-item
        
        
        
        ">
      <a href="/javascript/">
          <b>1. </b>— JavaScript
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
        <ul>
          
          
            
          
          

        
          
            
            




 
  
    
    <li data-nav-id="/javascript/%E8%AE%A1%E6%97%B6%E5%99%A8%E6%96%B9%E6%B3%95%E5%8F%8A%E5%85%B6%E7%9B%B8%E5%85%B3%E5%BA%94%E7%94%A8/" title="JS定时器应用" class="dd-item
        
        
        
        ">
      <a href="/javascript/%E8%AE%A1%E6%97%B6%E5%99%A8%E6%96%B9%E6%B3%95%E5%8F%8A%E5%85%B6%E7%9B%B8%E5%85%B3%E5%BA%94%E7%94%A8/">
          JS定时器应用
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/javascript/ajax/" title="Ajax简单介绍" class="dd-item
        
        
        
        ">
      <a href="/javascript/ajax/">
          Ajax简单介绍
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/javascript/%E5%AF%B9%E8%B1%A1%E6%8B%B7%E8%B4%9D/" title="JS对象拷贝" class="dd-item
        
        
        
        ">
      <a href="/javascript/%E5%AF%B9%E8%B1%A1%E6%8B%B7%E8%B4%9D/">
          JS对象拷贝
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/javascript/js%E5%86%85%E5%AD%98%E6%9C%BA%E5%88%B6/" title="JS中的内存机制" class="dd-item
        
        
        
        ">
      <a href="/javascript/js%E5%86%85%E5%AD%98%E6%9C%BA%E5%88%B6/">
          JS中的内存机制
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/javascript/%E9%81%8D%E5%8E%86%E6%96%B9%E6%B3%95/" title="JS遍历方法总结" class="dd-item
        
        
        
        ">
      <a href="/javascript/%E9%81%8D%E5%8E%86%E6%96%B9%E6%B3%95/">
          JS遍历方法总结
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
        
        </ul>
      
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/node.js/" title="— Node.js" class="dd-item
        parent
        
        
        ">
      <a href="/node.js/">
          <b>2. </b>— Node.js
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
        <ul>
          
          
            
          
          

        
          
            
            




 
  
    
      <li data-nav-id="/node.js/koa%E5%9F%BA%E7%A1%80/" title="Koa创建本地服务器" class="dd-item ">
        <a href="/node.js/koa%E5%9F%BA%E7%A1%80/">
        Koa创建本地服务器
        <i class="fas fa-check read-icon"></i>
        </a>
    </li>
     
  
 

            
          
            
            




 
  
    
      <li data-nav-id="/node.js/nunjucks%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E%E5%85%A5%E9%97%A8/" title="Nunjucks模板引擎常用基础" class="dd-item ">
        <a href="/node.js/nunjucks%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E%E5%85%A5%E9%97%A8/">
        Nunjucks模板引擎常用基础
        <i class="fas fa-check read-icon"></i>
        </a>
    </li>
     
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/node.js/nodemailer/" title="Nodemailer实现邮箱注册" class="dd-item
        
        
        
        ">
      <a href="/node.js/nodemailer/">
          Nodemailer实现邮箱注册
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/node.js/node%E5%8D%9A%E5%AE%A2%E7%B3%BB%E7%BB%9F/" title="Node博客系统开发记录" class="dd-item
        parent
        active
        
        ">
      <a href="/node.js/node%E5%8D%9A%E5%AE%A2%E7%B3%BB%E7%BB%9F/">
          Node博客系统开发记录
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/node.js/node-%E7%88%AC%E8%99%AB/" title="Node爬虫思路整理" class="dd-item
        
        
        
        ">
      <a href="/node.js/node-%E7%88%AC%E8%99%AB/">
          Node爬虫思路整理
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/node.js/cookiesession%E5%BA%94%E7%94%A8/" title="cookie,session实现登录验证" class="dd-item
        
        
        
        ">
      <a href="/node.js/cookiesession%E5%BA%94%E7%94%A8/">
          cookie,session实现登录验证
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/node.js/token%E5%AE%9E%E4%BE%8B/" title="Egg.js: token应用实例" class="dd-item
        
        
        
        ">
      <a href="/node.js/token%E5%AE%9E%E4%BE%8B/">
          Egg.js: token应用实例
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
      <li data-nav-id="/node.js/cookie-%E5%92%8C-session/" title="koa应用cookie,session实例" class="dd-item ">
        <a href="/node.js/cookie-%E5%92%8C-session/">
        koa应用cookie,session实例
        <i class="fas fa-check read-icon"></i>
        </a>
    </li>
     
  
 

            
          
        
        </ul>
      
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/vue/" title="— Vue.js" class="dd-item
        
        
        
        ">
      <a href="/vue/">
          <b>3. </b>— Vue.js
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
        <ul>
          
          
            
          
          

        
          
            
            




 
  
    
      <li data-nav-id="/vue/vue2.0%E8%B5%B7%E6%AD%A5/" title="vue基础入门" class="dd-item ">
        <a href="/vue/vue2.0%E8%B5%B7%E6%AD%A5/">
        vue基础入门
        <i class="fas fa-check read-icon"></i>
        </a>
    </li>
     
  
 

            
          
            
            




 
  
    
      <li data-nav-id="/vue/vue-%E7%BB%84%E4%BB%B6%E4%BC%A0%E5%80%BC/" title="vue组件传值总结" class="dd-item ">
        <a href="/vue/vue-%E7%BB%84%E4%BB%B6%E4%BC%A0%E5%80%BC/">
        vue组件传值总结
        <i class="fas fa-check read-icon"></i>
        </a>
    </li>
     
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/vue/vuerouter/" title="VueRouter常用点整理" class="dd-item
        
        
        
        ">
      <a href="/vue/vuerouter/">
          VueRouter常用点整理
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
        
        </ul>
      
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/web/" title="— Web About" class="dd-item
        
        
        
        ">
      <a href="/web/">
          <b>4. </b>— Web About
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
        <ul>
          
          
            
          
          

        
          
            
            




 
  
    
    <li data-nav-id="/web/indexeddb/" title="IndexedDB数据库常用函数封装" class="dd-item
        
        
        
        ">
      <a href="/web/indexeddb/">
          IndexedDB数据库常用函数封装
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
            
            




 
  
    
    <li data-nav-id="/web/url%E5%A4%84%E7%90%86/" title="URL手动处理" class="dd-item
        
        
        
        ">
      <a href="/web/url%E5%A4%84%E7%90%86/">
          URL手动处理
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
        
        </ul>
      
    </li>
  
 

          
          




 
  
    
    <li data-nav-id="/myimg/" title="— 杂 感 . . ." class="dd-item
        
        
        
        ">
      <a href="/myimg/">
          <b>5. </b>— 杂 感 . . .
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
        <ul>
          
          
            
          
          

        
          
            
            




 
  
    
    <li data-nav-id="/myimg/myimage/" title="Myimages" class="dd-item
        
        
        
        ">
      <a href="/myimg/myimage/">
          Myimages
          
            <i class="fas fa-check read-icon"></i>
          
      </a>
      
      
    </li>
  
 

            
          
        
        </ul>
      
    </li>
  
 

          
        
    </ul>

    
    
      <section id="shortcuts">
        <h3></h3>
        <ul>
          
              <li>
                  <a class="padding" href="https://gitee.com/wen-yun-li"><i class='fab fa-fw fa-github'></i> — Gitee</a>
              </li>
          
              <li>
                  <a class="padding" href="https://github.com/liwenyun"><i class='fab fa-fw fa-github'></i> — GitHub</a>
              </li>
          
              <li>
                  <a class="padding" href="https://www.runoob.com/"><i class='fas fa-fw fa-bookmark'></i>— 菜鸟教程</a>
              </li>
          
              <li>
                  <a class="padding" href="https://www.eggjs.org/zh-CN/intro/quickstart"><i class='fas fa-fw fa-bookmark'></i>— Egg.js 文档</a>
              </li>
          
              <li>
                  <a class="padding" href="https://www.axios-http.cn/docs/intro"><i class='fas fa-fw fa-bookmark'></i>— Axios 文档</a>
              </li>
          
              <li>
                  <a class="padding" href="https://v3.cn.vuejs.org/guide/introduction.html"><i class='fas fa-fw fa-bookmark'></i>— Vue.js 文档</a>
              </li>
          
        </ul>
      </section>
    

    
    <section id="prefooter">
      <hr/>
      <ul>
      

      
        <li><a class="padding" href="#" data-clear-history-toggle=""><i class="fas fa-history fa-fw"></i> </a></li>
      
      </ul>
    </section>
    
    <section id="footer">
      <center>
    
    <a class="github-button" href="https://github.com/matcornic/hugo-theme-learn/archive/master.zip" data-icon="octicon-cloud-download" aria-label="Download matcornic/hugo-theme-learn on GitHub">Download</a>

    
    <a class="github-button" href="https://github.com/matcornic/hugo-theme-learn" data-icon="octicon-star" data-show-count="true" aria-label="Star matcornic/hugo-theme-learn on GitHub">Star</a>

    
    <a class="github-button" href="https://github.com/matcornic/hugo-theme-learn/fork" data-icon="octicon-repo-forked" data-show-count="true" aria-label="Fork matcornic/hugo-theme-learn on GitHub">Fork</a>

    <p>Built with <a href="https://github.com/matcornic/hugo-theme-learn"><i class="fas fa-heart"></i></a> from <a href="https://getgrav.org">Grav</a> and <a href="https://gohugo.io/">Hugo</a></p>
</center>

<script async defer src="https://buttons.github.io/buttons.js"></script>

    </section>
  </div>
</nav>




        <section id="body">
        <div id="overlay"></div>
        <div class="padding highlightable">
              
              <div>
                <div id="top-bar">
                
                  
                  
                  
                  <div id="top-github-link">
                    <a class="github-link" title='' href="https://github.com/matcornic/hugo-theme-learn/edit/master/exampleSite/content/node.js/node%e5%8d%9a%e5%ae%a2%e7%b3%bb%e7%bb%9f/_index.zh.md" target="blank">
                      <i class="fas fa-code-branch"></i>
                      <span id="top-github-link-text"></span>
                    </a>
                  </div>
                  
                
                
                <div id="breadcrumbs" itemscope="" itemtype="http://data-vocabulary.org/Breadcrumb">
                    <span id="sidebar-toggle-span">
                        <a href="#" id="sidebar-toggle" data-sidebar-toggle="">
                          <i class="fas fa-bars"></i>
                        </a>
                    </span>
                  
                  <span id="toc-menu"><i class="fas fa-list-alt"></i></span>
                  
                  <span class="links">
                 
                 
                    
          
          
            
            
          
          
            
            
          
          
            <a href='/'>LIWENYUN BLOG</a> > <a href='/node.js/'>— Node.js</a> > Node博客系统开发记录
          
        
          
        
          
        
                 
                  </span>
                </div>
                
                    <div class="progress">
    <div class="wrapper">
<nav id="TableOfContents">
  <ul>
    <li><a href="#一--node-http开发思路">一、  node HTTP开发思路</a></li>
    <li><a href="#二--搭建开发环境">二、  搭建开发环境</a>
      <ul>
        <li><a href="#1-创建基本文件夹">1. 创建基本文件夹</a></li>
        <li><a href="#2-路由">2. 路由</a></li>
      </ul>
    </li>
    <li><a href="#三--处理异步代码">三 、 处理异步代码</a>
      <ul>
        <li><a href="#1-完善blogdetail路由">1. 完善blog/detail路由</a></li>
        <li><a href="#2-认识promise">2. 认识promise</a></li>
      </ul>
    </li>
    <li><a href="#四--异步处理post-数据">四、  异步处理post 数据</a></li>
    <li><a href="#五--开发新建更新删除博客的路由">五  开发新建，更新、删除博客的路由</a>
      <ul>
        <li><a href="#1-新建">1. 新建</a></li>
        <li><a href="#2-更新">2. 更新</a></li>
        <li><a href="#3-删除">3. 删除</a></li>
      </ul>
    </li>
    <li><a href="#六--连接mysql">六、  连接mySQL</a>
      <ul>
        <li><a href="#1-使用mysql增查">1. 使用mySQL(增、查)</a></li>
        <li><a href="#2-使用mysql改删">2. 使用mySQL(改、删)</a></li>
        <li><a href="#3-nodejs连接mysql">3. node.js连接mySQL</a></li>
      </ul>
    </li>
    <li><a href="#七--封装执行mysql语句的工具函数">七 、 封装执行mySQL语句的工具函数</a></li>
    <li><a href="#八--路由接口对接mysql">八、  路由接口对接mySQL</a>
      <ul>
        <li><a href="#1-获取博客列表对接mysql">1. 获取博客列表对接mySQL</a></li>
        <li><a href="#2-博客详情新建博客接口对接mysql">2. 博客详情、新建博客接口对接mySQL</a></li>
        <li><a href="#3-更新博客删除博客接口对接mysql">3. 更新博客、删除博客接口对接mySQL</a></li>
      </ul>
    </li>
  </ul>
</nav>
    </div>
</div>

                
              </div>
            </div>
            
        <div id="head-tags">
        
        </div>
        
        <div id="body-inner">
          
            <h1>
              
              Node博客系统开发记录
            </h1>
          

        



	<h2 id="一--node-http开发思路">一、  node HTTP开发思路</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">http</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;http&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">querystring</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;querystring&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">server</span><span style="color:#f92672">=</span> <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">createServer</span>((<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//获取请求信息
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">url</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">url</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>)[<span style="color:#ae81ff">0</span>];<span style="color:#75715e">//获取域名
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">query</span><span style="color:#f92672">=</span><span style="color:#a6e22e">querystring</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#34;?&#34;</span>)[<span style="color:#ae81ff">1</span>])<span style="color:#75715e">//获取get请求的参数
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//定义响应数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">responseData</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">method</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">url</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">path</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">query</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//将返回给客户端的响应数据responData的数据类型设置成JSON格式
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">setHeader</span>(<span style="color:#e6db74">&#39;Content-Type&#39;</span>,<span style="color:#e6db74">&#39;application/json&#39;</span>);
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//判断请求方法后响应数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">responseData</span>)
</span></span><span style="display:flex;"><span>        )
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">postData</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//通过on方法监听
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;data&#39;</span>,<span style="color:#a6e22e">chunk</span>=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">postData</span> <span style="color:#f92672">+=</span> <span style="color:#a6e22e">chunk</span>.<span style="color:#a6e22e">toString</span>();<span style="color:#75715e">//服务端接收到的请求数据是二进制形式，要对其进行流处理后拼接成字符串
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        })
</span></span><span style="display:flex;"><span>        
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;end&#39;</span>,()=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">responseData</span>.<span style="color:#a6e22e">postData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">postData</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">responseData</span>)
</span></span><span style="display:flex;"><span>                )
</span></span><span style="display:flex;"><span>        })
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>});
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">server</span>.<span style="color:#a6e22e">listen</span>(<span style="color:#ae81ff">3000</span>,()=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;server is runing at port 3000&#39;</span>)
</span></span><span style="display:flex;"><span>})
</span></span></code></pre></div><p>通过浏览器向服务器发送一个GET请求</p>
<p>路由为：</p>
<pre tabindex="0"><code>http://localhost:3000/blog/list?username=user&amp;password=pass
</code></pre><!-- raw HTML omitted -->
<p>通过Postman发送一个POST请求</p>
<p>路由为：</p>
<pre tabindex="0"><code>http://localhost:3000/blog/list?name=username
</code></pre><p>发送post数据：</p>
<pre tabindex="0"><code>{
    &#34;name&#34;:&#34;name&#34;,
    &#34;age&#34;:&#34;13&#34;

}
</code></pre><!-- raw HTML omitted -->
<h2 id="二--搭建开发环境">二、  搭建开发环境</h2>
<h3 id="1-创建基本文件夹">1. 创建基本文件夹</h3>
<ol>
<li>
<p>执行命令 npm init -y 会在根目录下生成一个package.json文件</p>
</li>
<li>
<p>执行命令 npm install nodemon -D 下载nodemon模块</p>
</li>
<li>
<p>在根目录下创建bin文件夹，在bin文件夹下创建www.js文件（</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//www.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">http</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;http&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">serverHandler</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../app.js&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">PORT</span><span style="color:#f92672">=</span><span style="color:#ae81ff">3000</span>;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">server</span><span style="color:#f92672">=</span><span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">createServer</span>(<span style="color:#a6e22e">serverHandler</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">server</span>.<span style="color:#a6e22e">listen</span>(<span style="color:#a6e22e">PORT</span>,()=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;server is running at port 3000&#39;</span>);
</span></span><span style="display:flex;"><span>})
</span></span></code></pre></div></li>
<li>
<p>对package.json文件做如下设置</p>
<pre tabindex="0"><code>//package.json
{
    &#34;name&#34;: &#34;development-environment&#34;,
    &#34;version&#34;: &#34;1.0.0&#34;,
    &#34;description&#34;: &#34;&#34;,
    &#34;main&#34;: &#34;bin/www.js&#34;,//将www.js文件设置成入口文件，即项目中第一个执行的文件
    &#34;scripts&#34;: {
        &#34;dev&#34;:&#34;nodemon bin/www.js&#34; //自定义npm命令,每次执行npm run dev的时候都会通过nodemon执行www.js文件
    },
    &#34;keywords&#34;: [],
    &#34;author&#34;: &#34;&#34;,
    &#34;license&#34;: &#34;ISC&#34;,
    &#34;devDependencies&#34;: {
        &#34;nodemon&#34;: &#34;^2.0.14&#34; //项目依赖，每次下载第三放依赖后，会自动添加。
    }
}
</code></pre></li>
<li>
<p>在根目录下创建app.js文件（服务器业务代码）</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//app.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">serverHandler</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">setHeader</span>(<span style="color:#e6db74">&#39;Content-Type&#39;</span>,<span style="color:#e6db74">&#39;applocation/json&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">responseData</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">name</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;name&#39;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">age</span><span style="color:#f92672">:</span><span style="color:#ae81ff">22</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">responseData</span>)
</span></span><span style="display:flex;"><span>    )
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span><span style="color:#a6e22e">serverHandler</span>;
</span></span></code></pre></div></li>
</ol>
<h3 id="2-路由">2. 路由</h3>
<h4 id="21-初始化路由">2.1 初始化路由</h4>
<p>在根目录下创建src文件夹，在src文件夹下创建routes文件夹，在该文件加下创建blog.js文件，该文件用来处理博客相关的路由。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//blog.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//处理博客相关的路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">url</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">url</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>)[<span style="color:#ae81ff">0</span>];
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/list&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;获取博客列表的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/detail&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;获取博客详情的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/new&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;新建博客的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/updata&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;更新博客的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/delete&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;删除博客列表的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">handleBlogRoute</span>;
</span></span></code></pre></div><p>app.js文件如下：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//app.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;./src/routes/blog&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">serverHandler</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">setHeader</span>(<span style="color:#e6db74">&#39;Content-Type&#39;</span>,<span style="color:#e6db74">&#39;applocation/json&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">handleBlogRoute</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">blogData</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">blogData</span>)
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//当客户端访问不存在的路由时，返回404报错信息
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">writeHead</span>(<span style="color:#ae81ff">404</span>,{<span style="color:#e6db74">&#39;Content-Type&#39;</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;text/plain&#39;</span>});
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#39;404 Not Found&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>();
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span><span style="color:#a6e22e">serverHandler</span>;
</span></span></code></pre></div><p>针对blog.js可以做一点小优化：</p>
<p>我们发现当存在很多路由文件时，下面的代码我们每次都要写一遍，</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">url</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">url</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>)[<span style="color:#ae81ff">0</span>];
</span></span></code></pre></div><p>在app.js中，将path赋值为req的一个属性，这样只要能访问到req对象就能拿到path。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//app.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;./src/routes/blog&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">serverHandler</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">setHeader</span>(<span style="color:#e6db74">&#39;Content-Type&#39;</span>,<span style="color:#e6db74">&#39;applocation/json&#39;</span>);
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//将path赋值为req对象的一个属性
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">url</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">url</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>)[<span style="color:#ae81ff">0</span>];
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">handleBlogRoute</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">blogData</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">blogData</span>)
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//当客户端访问不存在的路由时，返回404报错信息
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">writeHead</span>(<span style="color:#ae81ff">404</span>,{<span style="color:#e6db74">&#39;Content-Type&#39;</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;text/plain&#39;</span>});
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#39;404 Not Found&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>();
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>那么响应的路由文件中的path也必须改为req.path</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//blog.js 将path全部修改为req.path
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//处理博客相关的路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/list&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;获取博客列表的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/detail&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;获取博客详情的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/new&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;新建博客的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/updata&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;更新博客的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/delete&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;删除博客列表的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">handleBlogRoute</span>;
</span></span></code></pre></div><h4 id="2-2开发第一个路由bloglist">2. 2开发第一个路由（blog/list)</h4>
<p>在src文件夹下创建model文件夹，在该文件夹下创建responseModel.js文件</p>
<p>此文件中的代码用于格式化返回响应数据</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//responseModel.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">BaseModel</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">constructor</span>(<span style="color:#a6e22e">data</span>,<span style="color:#a6e22e">message</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (<span style="color:#66d9ef">typeof</span> <span style="color:#a6e22e">data</span> <span style="color:#f92672">===</span> <span style="color:#e6db74">&#39;string&#39;</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">message</span><span style="color:#f92672">=</span><span style="color:#a6e22e">data</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span><span style="color:#f92672">=</span><span style="color:#66d9ef">null</span>;
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">=</span><span style="color:#66d9ef">null</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">data</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">data</span><span style="color:#f92672">=</span><span style="color:#a6e22e">data</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">message</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">message</span><span style="color:#f92672">=</span><span style="color:#a6e22e">message</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//成功模型
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">SuccessModel</span> <span style="color:#66d9ef">extends</span> <span style="color:#a6e22e">BaseModel</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">constructor</span>(<span style="color:#a6e22e">data</span>,<span style="color:#a6e22e">message</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">super</span>(<span style="color:#a6e22e">data</span>,<span style="color:#a6e22e">message</span>); <span style="color:#75715e">//super关键字用于访问和调用一个对象的父对象上的函数。
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">errno</span><span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//失败模型
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ErrorModel</span> <span style="color:#66d9ef">extends</span> <span style="color:#a6e22e">BaseModel</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">constructor</span>(<span style="color:#a6e22e">data</span>,<span style="color:#a6e22e">message</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">super</span>(<span style="color:#a6e22e">data</span>,<span style="color:#a6e22e">message</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">errno</span><span style="color:#f92672">=-</span><span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">SuccessModel</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">ErrorModel</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>同时，完善一下路由文件blog.js中的blog/list路由</p>
<p>假设路由请求为</p>
<pre tabindex="0"><code>//api/blog/list?author=zhangsan&amp;keyword=123
</code></pre><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//routes/blog.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//处理博客相关的路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> { <span style="color:#a6e22e">SuccessModel</span>}<span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../model/responseModel&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> {<span style="color:#a6e22e">getList</span>}<span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../controllers/blog&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/list&#39;</span>){
</span></span><span style="display:flex;"><span>       <span style="color:#75715e">//api/blog/list?author=zhangsan&amp;keyword=123
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>       <span style="color:#75715e">//new SuccessModel() 
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">author</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">author</span><span style="color:#f92672">||</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">keyword</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">keyword</span><span style="color:#f92672">||</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">listData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">getList</span>(<span style="color:#a6e22e">author</span>,<span style="color:#a6e22e">keyword</span>);<span style="color:#75715e">//getList方法定义在controller下的blog.js中
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#a6e22e">listData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// return{
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//     message:&#39;获取博客列表的接口&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">// }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/detail&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;获取博客详情的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/new&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;新建博客的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/updata&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;更新博客的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/delete&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">message</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;删除博客列表的接口&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">handleBlogRoute</span>;
</span></span></code></pre></div><p>还需要在app.js文件中，获取并解析url中的参数，并将其赋值给req.query，bolg.js中才能调用。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;./src/routes/blog&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">querystring</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;querystring&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">serverHandler</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">setHeader</span>(<span style="color:#e6db74">&#39;Content-Type&#39;</span>,<span style="color:#e6db74">&#39;applocation/json&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//获取path
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">url</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">url</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>)[<span style="color:#ae81ff">0</span>];
</span></span><span style="display:flex;"><span>   
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//解析query
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span><span style="color:#f92672">=</span><span style="color:#a6e22e">querystring</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>[<span style="color:#ae81ff">1</span>]));
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">handleBlogRoute</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">blogData</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">blogData</span>)
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//当客户端访问不存在的路由时，返回404报错信息
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">writeHead</span>(<span style="color:#ae81ff">404</span>,{<span style="color:#e6db74">&#39;Content-Type&#39;</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;text/plain&#39;</span>});
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#39;404 Not Found&#39;</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>();
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span><span style="color:#a6e22e">serverHandler</span>;
</span></span></code></pre></div><p>路由中还用到一个getList方法，需要在src文件夹下创建一个controllers文件夹，在该文件夹下新建blog.js文件，在里面定义getList方法。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//controller/blog.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//获取博客列表数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">getList</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">author</span>,<span style="color:#a6e22e">keyword</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//此处的数据应该是从数据库里取得的
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#75715e">//但此处为便于演示，返回下列自定义数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">return</span> [
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">id</span><span style="color:#f92672">:</span><span style="color:#ae81ff">1</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">title</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;标题1&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">content</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;内容1&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">author</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;张三&#39;</span>
</span></span><span style="display:flex;"><span>        },
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">id</span><span style="color:#f92672">:</span><span style="color:#ae81ff">2</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">title</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;标题2&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">content</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;内容2&#39;</span>,
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">author</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;李四&#39;</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    ]
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">getList</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>postman测试：</p>
<p>发送路由</p>
<pre tabindex="0"><code>http://localhost:3000/api/blog/list?author=zhangsan&amp;keyword=123
</code></pre><p>测试结果：</p>
<p><img src="003.jpg" alt=""></p>
<h2 id="三--处理异步代码">三 、 处理异步代码</h2>
<h3 id="1-完善blogdetail路由">1. 完善blog/detail路由</h3>
<p>在conotroller/blog.js中添加获取博客详情的数据的代码</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">// 获取博客详情数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">getDetail</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">id</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//先返回假数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">return</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">id</span><span style="color:#f92672">:</span><span style="color:#ae81ff">1</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">title</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;标题1&#39;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">content</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;内容1&#39;</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">author</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;张三&#39;</span>
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>完善路由routes/blog.js</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> {<span style="color:#a6e22e">getList</span>, <span style="color:#a6e22e">getDetail</span>}<span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../controllers/blog&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/detail&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span> <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">id</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">detailData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">getDetail</span>(<span style="color:#a6e22e">id</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#a6e22e">detailData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// return{
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//     message:&#39;获取博客详情的接口&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">// }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    }
</span></span></code></pre></div><h3 id="2-认识promise">2. 认识promise</h3>
<p>接下来要处理的都是post请求，在处理之前先来认识promise</p>
<p>新建promise-demo文件夹，在其下面新建data文件夹（其下包含a.json,b.json,c.json文件)和index.js文件</p>
<p>在三个json文件中创建简单数据：</p>
<pre tabindex="0"><code>{
    &#34;message&#34;:&#34;这是a的内容&#34;,
    &#34;next&#34;:&#34;b.json&#34;
}
</code></pre><pre tabindex="0"><code>{
    &#34;message&#34;:&#34;这是b的内容&#34;,
    &#34;next&#34;:&#34;c.json&#34;
}
</code></pre><pre tabindex="0"><code>{
    &#34;message&#34;:&#34;这是c的内容&#34;,
    &#34;next&#34;:null
}
</code></pre><p>在index.js中编写代码取读取这三个json文件：</p>
<p>以下是 data.toString 和 JSON.parse(data.toString 输出的区别</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">fs</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;fs&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;path&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//读取文件内容
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">filename</span>,<span style="color:#a6e22e">callback</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//获取数据文件的绝对路径
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">fullFilename</span><span style="color:#f92672">=</span><span style="color:#a6e22e">path</span>.<span style="color:#a6e22e">resolve</span>(<span style="color:#a6e22e">__dirname</span>,<span style="color:#e6db74">&#39;data&#39;</span>,<span style="color:#a6e22e">filename</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//用fs模块读取数据文件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">fs</span>.<span style="color:#a6e22e">readFile</span>(<span style="color:#a6e22e">fullFilename</span>,(<span style="color:#a6e22e">err</span>,<span style="color:#a6e22e">data</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">err</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#a6e22e">err</span>);
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//console.log(data.toString());
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//{
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//&#34;message&#34;:&#34;这是a的内容&#34;,
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//&#34;next&#34;:null
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//}
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">toString</span>()))<span style="color:#75715e">//{ message: &#39;这是a的内容&#39;, next: &#39;b.json&#39; }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    })
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">getFileContent</span>(<span style="color:#e6db74">&#39;a.json&#39;</span>);
</span></span></code></pre></div><p>在调用getFileContent函数中加入回调函数</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">fs</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;fs&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;path&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//读取文件内容
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">filename</span>,<span style="color:#a6e22e">callback</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//获取数据文件的绝对路径
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">fullFilename</span><span style="color:#f92672">=</span><span style="color:#a6e22e">path</span>.<span style="color:#a6e22e">resolve</span>(<span style="color:#a6e22e">__dirname</span>,<span style="color:#e6db74">&#39;data&#39;</span>,<span style="color:#a6e22e">filename</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//用fs模块读取数据文件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">fs</span>.<span style="color:#a6e22e">readFile</span>(<span style="color:#a6e22e">fullFilename</span>,(<span style="color:#a6e22e">err</span>,<span style="color:#a6e22e">data</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">err</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#a6e22e">err</span>);
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">callback</span>(
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">toString</span>())
</span></span><span style="display:flex;"><span>        );
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//回调地狱
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">getFileContent</span>(<span style="color:#e6db74">&#39;a.json&#39;</span>,(<span style="color:#a6e22e">aData</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;aData&#39;</span>, <span style="color:#a6e22e">aData</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">aData</span>.<span style="color:#a6e22e">next</span>,(<span style="color:#a6e22e">bData</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;bData&#39;</span>,<span style="color:#a6e22e">bData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">bData</span>.<span style="color:#a6e22e">next</span>,(<span style="color:#a6e22e">cData</span>)=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;cData&#39;</span>,<span style="color:#a6e22e">cData</span>);
</span></span><span style="display:flex;"><span>        })
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>});
</span></span><span style="display:flex;"><span><span style="color:#75715e">//输出如下
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//aData { message: &#39;这是a的内容&#39;, next: &#39;b.json&#39; }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//bData { message: &#39;这是b的内容&#39;, next: &#39;c.json&#39; }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//cData { message: &#39;这是c的内容&#39;, next: null }
</span></span></span></code></pre></div><p>上述代码形成了回调地狱</p>
<p>不利于阅读和维护</p>
<p>利用promise改写上述代码：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">fs</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;fs&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> { <span style="color:#a6e22e">resolve</span> } <span style="color:#f92672">=</span> <span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;path&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;path&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">filename</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">promise</span><span style="color:#f92672">=</span><span style="color:#66d9ef">new</span> Promise((<span style="color:#a6e22e">resolve</span>,<span style="color:#a6e22e">reject</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">fullFilename</span><span style="color:#f92672">=</span><span style="color:#a6e22e">path</span>.<span style="color:#a6e22e">resolve</span>(<span style="color:#a6e22e">__dirname</span>,<span style="color:#e6db74">&#39;data&#39;</span>,<span style="color:#a6e22e">filename</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//用fs模块读取数据文件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">fs</span>.<span style="color:#a6e22e">readFile</span>(<span style="color:#a6e22e">fullFilename</span>,(<span style="color:#a6e22e">err</span>,<span style="color:#a6e22e">data</span>)=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">err</span>){
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#a6e22e">err</span>);
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">resolve</span>(
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">toString</span>())
</span></span><span style="display:flex;"><span>             );
</span></span><span style="display:flex;"><span>        }); 
</span></span><span style="display:flex;"><span>    });
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">promise</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">getFileContent</span>(<span style="color:#e6db74">&#39;a.json&#39;</span>).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">aData</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;aData&#39;</span>,<span style="color:#a6e22e">aData</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">aData</span>.<span style="color:#a6e22e">next</span>);
</span></span><span style="display:flex;"><span>}).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">bData</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;bData&#39;</span>,<span style="color:#a6e22e">bData</span>);
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">getFileContent</span>(<span style="color:#a6e22e">bData</span>.<span style="color:#a6e22e">next</span>);
</span></span><span style="display:flex;"><span>}).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">cData</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;aData&#39;</span>,<span style="color:#a6e22e">cData</span>);
</span></span><span style="display:flex;"><span>})
</span></span><span style="display:flex;"><span><span style="color:#75715e">/*输出如下
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">aData { message: &#39;这是a的内容&#39;, next: &#39;b.json&#39; }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">bData { message: &#39;这是b的内容&#39;, next: &#39;c.json&#39; }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">aData { message: &#39;这是c的内容&#39;, next: null }
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">*/</span>
</span></span></code></pre></div><h2 id="四--异步处理post-数据">四、  异步处理post 数据</h2>
<p>先复习之前以数据流的方式处理post请求的代码</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">postData</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;data&#39;</span>,<span style="color:#a6e22e">chunk</span>=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">postData</span><span style="color:#f92672">+=</span><span style="color:#a6e22e">chunk</span>.<span style="color:#a6e22e">toString</span>();
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;end&#39;</span>,()=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;postData&#39;</span>,<span style="color:#a6e22e">postData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(<span style="color:#e6db74">&#39;数据接收完毕&#39;</span>);
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在实际应用中我们需要以异步的方式来获取POST的数据</p>
<p>在app.js中添加一个异步获取post数据的函数getPostData()</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//app.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">//异步获取post数据的代码
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">getPostData</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">promise</span> <span style="color:#f92672">=</span><span style="color:#66d9ef">new</span> Promise((<span style="color:#a6e22e">resolve</span>,<span style="color:#a6e22e">reject</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#34;POST&#34;</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">resolve</span>({});
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">headers</span>[<span style="color:#e6db74">&#39;content-type&#39;</span>]<span style="color:#f92672">!==</span><span style="color:#e6db74">&#39;application/json&#39;</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">resolve</span>({});
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">postData</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;data&#39;</span>,<span style="color:#a6e22e">chunk</span>=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">postData</span><span style="color:#f92672">+=</span><span style="color:#a6e22e">chunk</span>.<span style="color:#a6e22e">toString</span>();
</span></span><span style="display:flex;"><span>        })
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// req.on(&#39;end&#39;,()=&gt;{
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//     console.log(&#39;postData&#39;,postData);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">//     res.end(&#39;数据接收完毕&#39;);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">// })
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;end&#39;</span>,()=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span>(<span style="color:#f92672">!</span><span style="color:#a6e22e">postData</span>){
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">resolve</span>({});
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">resolve</span>(
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">postData</span>)
</span></span><span style="display:flex;"><span>            );
</span></span><span style="display:flex;"><span>        });
</span></span><span style="display:flex;"><span>    });
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">promise</span>;
</span></span><span style="display:flex;"><span>};
</span></span></code></pre></div><p>这个方法在哪执行呢？它要在处理路由之前获取post数据，所以应该在app.js中处理路由代码的函数之前执行，但该方法是一个异步操作，获取Post数据的时候下面的路由时函数会先执行，所以不能简单的将其放在路由函数之前，而是应该将处理路由的函数放到异步函数的then代码块中去执行。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//app.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">serverHandler</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//设置相应格式
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">setHeader</span>(<span style="color:#e6db74">&#39;Content-Type&#39;</span>,<span style="color:#e6db74">&#39;applocation/json&#39;</span>);
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//获取path
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">url</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">url</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">=</span><span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>)[<span style="color:#ae81ff">0</span>];
</span></span><span style="display:flex;"><span>   
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//解析query
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span><span style="color:#f92672">=</span><span style="color:#a6e22e">querystring</span>.<span style="color:#a6e22e">parse</span>(<span style="color:#a6e22e">url</span>.<span style="color:#a6e22e">split</span>(<span style="color:#e6db74">&#39;?&#39;</span>[<span style="color:#ae81ff">1</span>]));
</span></span><span style="display:flex;"><span>    
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//异步获取post数据并执行路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#a6e22e">getPostData</span>(<span style="color:#a6e22e">req</span>).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">postData</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//将post数据赋值给请求，方便其他路由调用。
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span><span style="color:#f92672">=</span><span style="color:#a6e22e">postData</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//博客相关路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">handleBlogRoute</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">blogData</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">blogData</span>)
</span></span><span style="display:flex;"><span>            );
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//当客户端访问不存在的路由时，返回404报错信息
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">writeHead</span>(<span style="color:#ae81ff">404</span>,{<span style="color:#e6db74">&#39;Content-Type&#39;</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;text/plain&#39;</span>});
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#39;404 Not Found&#39;</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>();
</span></span><span style="display:flex;"><span>    })    
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="五--开发新建更新删除博客的路由">五  开发新建，更新、删除博客的路由</h2>
<h3 id="1-新建">1. 新建</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">id</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//新建博客路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/new&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newBlogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">createNewBlog</span>(<span style="color:#a6e22e">blogData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#a6e22e">newBlogData</span>);
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>同时在controllers.js中创建createNewBlog方法</p>
<h3 id="2-更新">2. 更新</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">id</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//更新博客路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/update&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">updatedBlogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">updateBlog</span>(<span style="color:#a6e22e">id</span>,<span style="color:#a6e22e">blogData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">updatedBlogData</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#e6db74">&#39;更新博客成功!&#39;</span>);
</span></span><span style="display:flex;"><span>        }<span style="color:#66d9ef">else</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">ErrorModel</span>(<span style="color:#e6db74">&#39;更新博客失败...&#39;</span>);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>同时在controllers.js中创建updateBlog方法</p>
<h3 id="3-删除">3. 删除</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">id</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//删除博客路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/delete&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">deleteBlogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">deleteBlog</span>(<span style="color:#a6e22e">id</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">deleteBlogData</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#e6db74">&#39;博客删除成功!&#39;</span>);
</span></span><span style="display:flex;"><span>        }<span style="color:#66d9ef">else</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">ErrorModel</span>(<span style="color:#e6db74">&#39;博客删除失败...&#39;</span>);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>同时在controllers.js中创建deleteBlog方法</p>
<h2 id="六--连接mysql">六、  连接mySQL</h2>
<p>数据库相关：</p>
<pre tabindex="0"><code>连接数据库：mysql -u root -p
查看数据库：show databases
创建数据库：create database dbname
切换数据库：use dbname
查看表：show tables
删除数据库：drop database dbname
修改数据库：alter database dbname character set utf8
</code></pre><p>表相关：</p>
<pre tabindex="0"><code>创建表：create table tbname(id int,name varchar(20),sex char(1));
查看表信息：desc tbname;
查询表数据：select * from tbname;
插入数据：insert into tbname values(1,&#39;小明&#39;,0);
修改数据：update tbname set name=&#39;小明&#39;,age=2 where id=3;
删除数据：delete from tbname where id=3;
添加表属性：alter table tbname add achievement double(5,2);
修改表属性：alter table tbname change name username varchar(20);
删除表属性：alter table tbname drop name
删除表：drop table tbname
</code></pre><p>数据类型</p>
<pre tabindex="0"><code>int 整型
double 浮点型 (double(6,3)，最长长度是6位，小数点后面3位，999.999)
char 固定长度字符串类型，(char(10),不足10位会补足10位，例如身份证)
varchar 可变长度字符串，(char(10),不足10位不会自动不足，性能低于char，例如个性签名)
text 长文本数据
date 日期类型
</code></pre><h3 id="1-使用mysql增查">1. 使用mySQL(增、查)</h3>
<pre tabindex="0"><code>use myblog;  -- 使用myblog数据库
-- 增
insert into blogs(title, content, author, createdAt) values (&#39;标题1&#39;, &#39;内容1&#39;, &#39;张三&#39;, 1234567890123); -- 向blogs表中插入数据

-- 查
select * from blogs;
select id, author from blogs;
select * from blogs where title=&#39;标题1&#39; and author=&#39;张三&#39;;
select * from blogs where title=&#39;标题1&#39; or author=&#39;张三&#39;;

-- 模糊查询
select * from blogs where title like &#39;%1%&#39;;
select * from blogs where author like &#39;%张%&#39; order by id; 
-- 查询作者名字中带“张”的博客，并按id顺序排列
select * from blogs where author like &#39;%张%&#39; order by id desc;
-- 查询作者名字中带“张”的博客，并按id倒序排列
</code></pre><h3 id="2-使用mysql改删">2. 使用mySQL(改、删)</h3>
<pre tabindex="0"><code>-- 改（更新）
set SQL_SAFE_UPDATES=0;  -- 执行该语句使数据库允许修改表
update blogs set title=&#39;标题3&#39; where content=&#39;内容1&#39;;
-- 将内容1的博客的标题修改位标题3

-- 删
delete from blogs -- 执行该语句会将整个表删除，所以一般情况下使不允许使用的
delete from blogs where title=&#39;标题2&#39;;

-- 软删除：
-- 在表的所有哦元组中添加一个‘state’属性，默认值设为‘1’；
-- 当要“删除”某一行时，直接执行：
update blogs set state=&#39;0&#39; where title=&#39;标题1&#39;;
-- 然后执行查找的时候：
select * from blogs where state=&#39;1&#39;; -- 或者
select * from blogs where state&lt;&gt;0; -- &lt;&gt;就是‘不等于’的意思
-- 可以发现‘title’为‘标题1’的文章已经查找不到了，但是那条数据并没有从数据库中彻底删除
-- 当需要恢复该条数据时，只需把‘state’的值改回‘1’就可以了。
</code></pre><h3 id="3-nodejs连接mysql">3. node.js连接mySQL</h3>
<p>demo演示：</p>
<ol>
<li>
<p>首先创建一个mysql-demo文件夹，文件夹下创建一个index.js 空文件</p>
</li>
<li>
<p>执行 cd mysql-demo 进入该文件夹</p>
</li>
<li>
<p>执行 npm init -y 进行初始化，会在文件夹下生成新的package.json文件</p>
</li>
<li>
<p>执行 npm install mysql 安装MySQL工具</p>
</li>
<li>
<p>在index.js中编写以下代码连接数据库：</p>
</li>
</ol>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">mysql</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span> (<span style="color:#e6db74">&#39;mysql&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//创建连接对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">connection</span><span style="color:#f92672">=</span><span style="color:#a6e22e">mysql</span>.<span style="color:#a6e22e">createConnection</span>({
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">host</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;localhost&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">user</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;root&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">password</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;111111&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">port</span><span style="color:#f92672">:</span><span style="color:#ae81ff">3306</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">database</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;myblog&#39;</span>
</span></span><span style="display:flex;"><span>});
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//开始连接
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">connect</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//执行sql语句
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`select * from blogs`</span>;
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">query</span>(<span style="color:#a6e22e">sql</span>,(<span style="color:#a6e22e">err</span>,<span style="color:#a6e22e">result</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">err</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#e6db74">&#39;error&#39;</span>,<span style="color:#a6e22e">error</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;result&#39;</span>,<span style="color:#a6e22e">result</span>);
</span></span><span style="display:flex;"><span>})
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//关闭连接
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">end</span>();
</span></span></code></pre></div><h2 id="七--封装执行mysql语句的工具函数">七 、 封装执行mySQL语句的工具函数</h2>
<p>在项目src文件夹下创建一个新文件夹database，并在该文件夹下创建mysql.js文件</p>
<p>在根目录下执行 npm install mysql 下载mysql工具</p>
<p>在src文件夹下创建config文件夹用于存放配置文件</p>
<p>在config文件夹下新建ab.js文件用于存储数据库基本配置</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//config/ab.js
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">let</span> <span style="color:#a6e22e">MYSQL_CONFIG</span><span style="color:#f92672">=</span>{};
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">MYSQL_CONFIG</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">host</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;localhost&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">user</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;root&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">password</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;111111&#39;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">port</span><span style="color:#f92672">:</span><span style="color:#ae81ff">3306</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">database</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;myblog&#39;</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">MYSQL_CONFIG</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>演示：</p>
<ol>
<li>
<p>在mysql.js文件中：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">mysql</span><span style="color:#f92672">=</span><span style="color:#a6e22e">require</span> (<span style="color:#e6db74">&#39;mysql&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> {<span style="color:#a6e22e">MYSQL_CONFIG</span>}<span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../config/db&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//创建连接对象
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">connection</span><span style="color:#f92672">=</span><span style="color:#a6e22e">mysql</span>.<span style="color:#a6e22e">createConnection</span>(<span style="color:#a6e22e">MYSQL_CONFIG</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//开始连接
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">connect</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>,<span style="color:#a6e22e">callback</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">query</span>(<span style="color:#a6e22e">sql</span>,<span style="color:#a6e22e">callback</span>);
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">module</span>.<span style="color:#a6e22e">exports</span><span style="color:#f92672">=</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">execSQL</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></li>
<li>
<p>在路由文件blog.js中：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span>{<span style="color:#a6e22e">execSQL</span>}<span style="color:#f92672">=</span><span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../db/mysql&#39;</span>);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">handleBlogRoute</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">method</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">method</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">id</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogData</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/list&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`select * from blogs`</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">query</span>(<span style="color:#a6e22e">sql</span>,(<span style="color:#a6e22e">err</span>,<span style="color:#a6e22e">result</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">err</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(<span style="color:#e6db74">&#39;error&#39;</span>,<span style="color:#a6e22e">error</span>);
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;result&#39;</span>,<span style="color:#a6e22e">result</span>);
</span></span><span style="display:flex;"><span>       })
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>当访问该路由时，就可以返回数据库中表blogs中的所有数据。</p>
</li>
</ol>
<p>优化：</p>
<p>现在我们封装的execSQL函数其实已经满足了执行SQL语句的目的，但还有可以优化的地方。</p>
<p>上面的演示中，通过回调函数执行SQL语句进行数据库操作，但当需要进行的操作很多时，就会形成回调地狱，我们可以通过promise来解决。</p>
<p>重写mysql.js中的execSQL函数：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">promise</span><span style="color:#f92672">=</span><span style="color:#66d9ef">new</span> Promise((<span style="color:#a6e22e">resolve</span>,<span style="color:#a6e22e">reject</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">connection</span>.<span style="color:#a6e22e">query</span>(<span style="color:#a6e22e">sql</span>,(<span style="color:#a6e22e">err</span>,<span style="color:#a6e22e">result</span>)=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">err</span>){
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">reject</span>(<span style="color:#a6e22e">err</span>);
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">resolve</span>(<span style="color:#a6e22e">result</span>);
</span></span><span style="display:flex;"><span>        })
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">promise</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>然后我们在路由文件中调用execSQL函数就可以更加简便：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/list&#39;</span>){
</span></span><span style="display:flex;"><span>       <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`select * from blogs`</span>;
</span></span><span style="display:flex;"><span>       <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>).<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">result</span>=&gt;{
</span></span><span style="display:flex;"><span>           <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;result&#39;</span>,<span style="color:#a6e22e">result</span>)
</span></span><span style="display:flex;"><span>       })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><h2 id="八--路由接口对接mysql">八、  路由接口对接mySQL</h2>
<h3 id="1-获取博客列表对接mysql">1. 获取博客列表对接mySQL</h3>
<p>将execSQL引入到controllers/blog.js文件中，并利用字符串拼接完成getBlogList方法的逻辑。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> { <span style="color:#a6e22e">execSQL</span> } <span style="color:#f92672">=</span> <span style="color:#a6e22e">require</span>(<span style="color:#e6db74">&#39;../database/mysql&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//获取博客列表数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">getBlogList</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">author</span>,<span style="color:#a6e22e">keyword</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`select * from blogs where 1=1 `</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//这里的1+1没有实际意义，为了防止路由后面没有传author和keyword时报错，此外注意在末尾要加空格。
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">author</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">sql</span><span style="color:#f92672">+=</span><span style="color:#e6db74">`and author=&#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">author</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39; `</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">keyword</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">sql</span><span style="color:#f92672">+=</span><span style="color:#e6db74">`and title like &#39;%</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">keyword</span><span style="color:#e6db74">}</span><span style="color:#e6db74">%&#39; `</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>);
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>因为execSQL返回的时一个promise对象，所以路由文件中对getBlogList方法的使用也有变化：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#75715e">//获取博客列表路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/list&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">author</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">auther</span><span style="color:#f92672">||</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">keyword</span><span style="color:#f92672">=</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">query</span>.<span style="color:#a6e22e">keyword</span><span style="color:#f92672">||</span><span style="color:#e6db74">&#39;&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">listDataPromise</span><span style="color:#f92672">=</span><span style="color:#a6e22e">getBlogList</span>(<span style="color:#a6e22e">author</span>,<span style="color:#a6e22e">keyword</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">listDataPromise</span>.<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">listData</span>)=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#a6e22e">listData</span>);
</span></span><span style="display:flex;"><span>        });
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>这里的listDataPromise.then之所以要返回出来，是因为在app.js中调用路由控制函数handleBlogRoute需要这里返回promise对象，以便于通过then输出blogData。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#a6e22e">getPostData</span>(<span style="color:#a6e22e">req</span>).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">postData</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//将post数据赋值给请求，方便其他路由调用。
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span><span style="color:#f92672">=</span><span style="color:#a6e22e">postData</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//博客相关路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">blogDataPromise</span><span style="color:#f92672">=</span><span style="color:#a6e22e">handleBlogRoute</span>(<span style="color:#a6e22e">req</span>,<span style="color:#a6e22e">res</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">blogDataPromise</span>){
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">blogDataPromise</span>.<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">blogData</span>=&gt;{
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">end</span>(<span style="color:#a6e22e">JSON</span>.<span style="color:#a6e22e">stringify</span>(<span style="color:#a6e22e">blogData</span>)); 
</span></span><span style="display:flex;"><span>            }); 
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span>;      
</span></span><span style="display:flex;"><span>        }
</span></span></code></pre></div><h3 id="2-博客详情新建博客接口对接mysql">2. 博客详情、新建博客接口对接mySQL</h3>
<p>完善getBlogDetail方法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">// 获取博客详情数据
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">getBlogDetail</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">id</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`select * from blogs where id=&#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">id</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;`</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">rows</span>)=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">rows</span>[<span style="color:#ae81ff">0</span>];<span style="color:#75715e">//rows时一个返回的对象数组，只有一个对象，所以通过返回row[0]就可以了
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>完善获取博客详情的路由：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#75715e">//获取博客详情路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;GET&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/detail&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// const detailData=getBlogDetail(id);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">// return new SuccessModel(detailData);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">detailDataPromise</span><span style="color:#f92672">=</span><span style="color:#a6e22e">getBlogDetail</span>(<span style="color:#a6e22e">id</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">detailDataPromise</span>.<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">detailData</span>=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#a6e22e">detailData</span>);
</span></span><span style="display:flex;"><span>        })
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>完善createNewBlog方法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//创建新的博客
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">createNewBlog</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">blogData</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">title</span><span style="color:#f92672">=</span><span style="color:#a6e22e">blogData</span>.<span style="color:#a6e22e">title</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">content</span><span style="color:#f92672">=</span><span style="color:#a6e22e">blogData</span>.<span style="color:#a6e22e">content</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">author</span><span style="color:#f92672">=</span><span style="color:#a6e22e">blogData</span>.<span style="color:#a6e22e">author</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">createdAt</span><span style="color:#f92672">=</span>Date.<span style="color:#a6e22e">now</span>();
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`insert into blogs (title, content, author, createdAt) values(&#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">title</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;, &#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">content</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;, &#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">author</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;, </span><span style="color:#e6db74">${</span><span style="color:#a6e22e">createdAt</span><span style="color:#e6db74">}</span><span style="color:#e6db74">);`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>).<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">insertedResult</span>=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;insertedResult&#39;</span>,<span style="color:#a6e22e">insertedResult</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span>{
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">id</span><span style="color:#f92672">:</span><span style="color:#a6e22e">insertedResult</span>.<span style="color:#a6e22e">insertId</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>完善新建博客路由：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#75715e">//新建博客路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/new&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// const newBlogData=createNewBlog(blogData);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#75715e">// return new SuccessModel(newBlogData);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">author</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;用户&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">body</span>.<span style="color:#a6e22e">author</span><span style="color:#f92672">=</span><span style="color:#a6e22e">author</span>;<span style="color:#75715e">//这里用一个假数据代替登录后的作者名
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newBlogDataPromise</span><span style="color:#f92672">=</span><span style="color:#a6e22e">createNewBlog</span>(<span style="color:#a6e22e">blogData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">newBlogDataPromise</span>.<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">newBlogData</span>=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#a6e22e">newBlogData</span>);
</span></span><span style="display:flex;"><span>        })
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><h3 id="3-更新博客删除博客接口对接mysql">3. 更新博客、删除博客接口对接mySQL</h3>
<p>完善updateBlog方法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//更新博客
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">updateBlog</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">id</span>,<span style="color:#a6e22e">blogData</span>)=&gt;{  
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">title</span><span style="color:#f92672">=</span><span style="color:#a6e22e">blogData</span>.<span style="color:#a6e22e">title</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">content</span><span style="color:#f92672">=</span><span style="color:#a6e22e">blogData</span>.<span style="color:#a6e22e">content</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`update blogs set title=&#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">title</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;, content=&#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">content</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39; where id=</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">id</span><span style="color:#e6db74">}</span><span style="color:#e6db74">;`</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>).<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">updateResult</span>=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;updateResult&#39;</span>,<span style="color:#a6e22e">updateResult</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">updateResult</span>.<span style="color:#a6e22e">affectedRows</span><span style="color:#f92672">&gt;</span><span style="color:#ae81ff">0</span>){<span style="color:#75715e">//该属性大于0的时候证明成功了
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>完善更新博客路由：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span>    <span style="color:#75715e">//更新博客路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/update&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// const updatedBlogData=updateBlog(id,blogData);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">updatedBlogDataPromise</span><span style="color:#f92672">=</span><span style="color:#a6e22e">updateBlog</span>(<span style="color:#a6e22e">id</span>,<span style="color:#a6e22e">blogData</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">updatedBlogDataPromise</span>.<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">updatedBlogData</span>=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">updatedBlogData</span>){
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#e6db74">&#39;更新博客成功!&#39;</span>);
</span></span><span style="display:flex;"><span>            }<span style="color:#66d9ef">else</span>{
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">ErrorModel</span>(<span style="color:#e6db74">&#39;更新博客失败...&#39;</span>);
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        });   
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div><p>完善deleteBlog方法：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">deleteBlog</span><span style="color:#f92672">=</span>(<span style="color:#a6e22e">id</span>,<span style="color:#a6e22e">author</span>)=&gt;{
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">//实际开发中尽量使用update进行软删除
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">sql</span><span style="color:#f92672">=</span><span style="color:#e6db74">`delete from blogs where id=</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">id</span><span style="color:#e6db74">}</span><span style="color:#e6db74"> and author=&#39;</span><span style="color:#e6db74">${</span><span style="color:#a6e22e">author</span><span style="color:#e6db74">}</span><span style="color:#e6db74">&#39;`</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">execSQL</span>(<span style="color:#a6e22e">sql</span>).<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">deleteResult</span>=&gt;{
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#39;updateResult&#39;</span>,<span style="color:#a6e22e">deleteResult</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">deleteResult</span>.<span style="color:#a6e22e">affectedRows</span><span style="color:#f92672">&gt;</span><span style="color:#ae81ff">0</span>){<span style="color:#75715e">//该属性大于0的时候证明成功了
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>            <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>;
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>完善删除博客路由：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#75715e">//删除博客路由
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">method</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;POST&#39;</span><span style="color:#f92672">&amp;&amp;</span><span style="color:#a6e22e">req</span>.<span style="color:#a6e22e">path</span><span style="color:#f92672">===</span><span style="color:#e6db74">&#39;/api/blog/delete&#39;</span>){
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">author</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#39;用户名&#39;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">deleteBlogDataPromise</span><span style="color:#f92672">=</span><span style="color:#a6e22e">deleteBlog</span>(<span style="color:#a6e22e">id</span>,<span style="color:#a6e22e">author</span>);
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">deleteBlogDataPromise</span>.<span style="color:#a6e22e">then</span>(<span style="color:#a6e22e">deleteBlogData</span>=&gt;{
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">deleteBlogData</span>){
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">SuccessModel</span>(<span style="color:#e6db74">&#39;博客删除成功!&#39;</span>);
</span></span><span style="display:flex;"><span>            }<span style="color:#66d9ef">else</span>{
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">ErrorModel</span>(<span style="color:#e6db74">&#39;博客删除失败...&#39;</span>);
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        });     
</span></span><span style="display:flex;"><span>    }
</span></span></code></pre></div>




<footer class=" footline" >
	
</footer> 

         
        </div>
        

      </div>

    <div id="navigation">
        
        

        
            
            
                
                    
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                        
                        
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
                        
            
            
                
                    
                
                

                    
                    
                        
                    
                    

                    
                        
            
            
                
                    
                
                

                    
                    
                    

                    
            
        
                    
            
        
                    
            
        

        


	 
	 
		
			<a class="nav nav-prev" href="/node.js/nodemailer/" title="Nodemailer实现邮箱注册"> <i class="fa fa-chevron-left"></i></a>
		
		
			<a class="nav nav-next" href="/node.js/node-%E7%88%AC%E8%99%AB/" title="Node爬虫思路整理" style="margin-right: 0px;"><i class="fa fa-chevron-right"></i></a>
		
	
    </div>

    </section>

    <div style="left: -1000px; overflow: scroll; position: absolute; top: -1000px; border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;">
      <div style="border: none; box-sizing: content-box; height: 200px; margin: 0px; padding: 0px; width: 200px;"></div>
    </div>
    <script src="/js/clipboard.min.js?1648731304"></script>
    <script src="/js/perfect-scrollbar.min.js?1648731304"></script>
    <script src="/js/perfect-scrollbar.jquery.min.js?1648731304"></script>
    <script src="/js/jquery.sticky.js?1648731304"></script>
    <script src="/js/featherlight.min.js?1648731304"></script>
    <script src="/js/highlight.pack.js?1648731304"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    <script src="/js/modernizr.custom-3.6.0.js?1648731304"></script>
    <script src="/js/learn.js?1648731304"></script>
    <script src="/js/hugo-learn.js?1648731304"></script>
    
        
            <script src="https://unpkg.com/mermaid@8.8.0/dist/mermaid.min.js"></script>
        
        <script>
            mermaid.initialize({ startOnLoad: true });
        </script>
    
    <script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-105947713-1', 'auto');
  ga('send', 'pageview');

</script>
  </body>
</html>
 